// pages/purchase-order-detail/index.js
const { globalData } = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    imagePrefix: globalData.imageUrl,
    order: {},
    total_price: "",
    memo: "",
    scrollHeight: 60,
  },

  handlePriceChange(event) {
    this.setData({
      total_price: event.detail.value,
    });
  },

  handleMemoChange(event) {
    this.setData({
      memo: event.detail.value,
    });
  },

  handleBlur(event) {
    this.setData({
      scrollHeight: 60,
    });

    wx.pageScrollTo({
      scrollTop: event.currentTarget.offsetTop,
    });
  },

  handleFocus(event) {
    const that = this;
    wx.createSelectorQuery()
      .select("#submit-bar")
      .boundingClientRect(function (rect) {
        that.setData({
          scrollHeight: event.detail.height - rect.height,
        });

        wx.pageScrollTo({
          scrollTop:
            event.currentTarget.offsetTop + event.detail.height - rect.height,
        });
      })
      .exec();
  },

  handleSubmit() {
    const { order_id, total_price, memo } = this.data;

    if (!total_price) {
      wx.showToast({
        title: "请输入总金额",
        icon: "none",
        duration: 1000,
      });

      return;
    }

    const param = { id: order_id, total_price, memo };

    wx.ajax.post("proccessPurchaseOrder.action", param, true).then((res) => {
      if (res.success) {
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 1000,
        });

        const timer = setTimeout(() => {
          wx.navigateBack({
            delat: 1,
          });

          clearTimeout(timer);
        }, 1000);
      } else {
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 1000,
        });
      }
    });
  },

  getData() {
    const { order_id } = this.data;

    const param = { id: order_id };

    wx.ajax.post("queryPurchaseOrderDetail.action", param, true).then((res) => {
      // console.log(res);
      if (res.success) {
        this.setData({
          order: res.result,
          total_price: res.result.order_total_price
            ? (res.result.order_total_price / 100).toFixed(2)
            : "",
          memo: res.result.memo || "",
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      order_id: options.id,
    });

    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
